import Vue from 'vue'
import store from '@/store/index.js'
import router from '@/router/index.js'
import component from './index.vue'

export default (data = {}) => {
  const Constructor = Vue.extend({
    router,
    store,
    render: h => h(component)
  })

  return new Promise((resolve, reject) => {
    let instance = new Constructor({
      el: document.createElement('div'),
      data
    })

    instance.$children[0].$on('login', () => {
      // 重新注册路由
      store.dispatch('sys/getDynamicRoutes')
      instance.$el.remove()
      resolve()
    })
    document.body.appendChild(instance.$el)
  })
}
